
package com.jacp.image;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * @desc 启动界面（选择照片界面）
 * @author <a href="mailto:likebamboo@163.com"
 *         target="_blank">likebamboo@163.com</a>
 * @create 2013-9-4
 * @Copyright 2013 <a href="http://liwentao.sinaapp.com"
 *            target="_blank">liwentao.sinaapp.com</a> Inc. All rights reserved.
 */
public class ChooseImageActivity extends Activity {

    private static final int FLAG_CHOOSE = 1;

    private static final int FLAG_HANDLEBACK = 2;

    private ImageView mImageView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choose);

        mImageView = (ImageView)findViewById(R.id.image);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.choose_img:
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent, FLAG_CHOOSE);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && null != data) {
            switch (requestCode) {
                case FLAG_CHOOSE:
                    Uri uri = data.getData();
                    Log.d("may", "uri=" + uri + ", authority=" + uri.getAuthority());
                    if (!TextUtils.isEmpty(uri.getAuthority())) {
                        Cursor cursor = getContentResolver().query(uri, new String[] {
                            MediaStore.Images.Media.DATA
                        }, null, null, null);
                        if (null == cursor) {
                            Toast.makeText(this, R.string.no_found, Toast.LENGTH_SHORT).show();
                            return;
                        }
                        cursor.moveToFirst();
                        String path = cursor.getString(cursor
                                .getColumnIndex(MediaStore.Images.Media.DATA));
                        Log.d("may", "path=" + path);
                        Intent intent = new Intent(this, MainActivity.class);
                        intent.putExtra("path", path);
                        startActivity(intent);
                    } else {
                        Log.d("may", "path=" + uri.getPath());
                        Intent intent = new Intent(this, MainActivity.class);
                        intent.putExtra("path", uri.getPath());
                        startActivity(intent);
                    }
                    break;
                case FLAG_HANDLEBACK:
                    String imagePath = data.getStringExtra("path");
                    Log.d("may", imagePath);
                    mImageView.setImageBitmap(BitmapFactory.decodeFile(imagePath));
                    break;
            }
        }

    }

}
